package com.agriculture.competition.task;

import cn.hutool.core.collection.CollectionUtil;
import com.agriculture.competition.domain.JaCompetitionManage;
import com.agriculture.competition.mapper.JaCompetitionManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author sgb
 * @create 2025/5/8
 */
@Component
public class CompetitionTask {

    @Autowired
    private JaCompetitionManageMapper jaCompetitionManageMapper;

   @Scheduled(cron = "0 0 3 * * ?")
 // @Scheduled(cron = "0 */3 * * * ?")
    public void competitionOver() {

        List<JaCompetitionManage> jaCompetitionManages = jaCompetitionManageMapper.queryAllCompetition();
        if (CollectionUtil.isNotEmpty(jaCompetitionManages)) {
            for (JaCompetitionManage mad : jaCompetitionManages) {
                Date registrationDeadline = mad.getRegistrationDeadline();
                // 选宿最后的截止时间增加15天
                LocalDate localDate = registrationDeadline.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                LocalDate nextMonth = localDate.plusDays(15);
                Date nextMonthDate = Date.from(nextMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
                if (nextMonthDate.getTime() < System.currentTimeMillis()) {
                    JaCompetitionManage jaCompetitionManage = new JaCompetitionManage();
                    jaCompetitionManage.setStatus("2");
                    jaCompetitionManage.setId(mad.getId());
                    jaCompetitionManageMapper.updateJaCompetitionManage(jaCompetitionManage);
                }

            }
        }
    }
}
